//
//  MYShopDetailsExplainView.m
//  MuYing
//
//  Created by tbb on 2019/7/30.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYShopDetailsExplainView.h"
#define BGVIEW_H 400

@interface MYShopDetailsExplainView ()
@property (nonatomic, strong) UIView * bgView;
@property (nonatomic, strong) MYShopDetailsSpecificationsSelectBottonBtn * bottonView;
@property (nonatomic, strong) UILabel * titleLb;
@property (nonatomic, strong) NSMutableArray * titleArr;
@end

@implementation MYShopDetailsExplainView

+ (void)showShopDetailsExplainViewWithModel:(YQPSkuInfoModel *)model {
    MYShopDetailsExplainView *screenView = [[MYShopDetailsExplainView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    screenView.backgroundColor = HexColorA(0x000000, 0.2);//HexColor(0xF3F5F9);
    screenView.model = model;
    [screenView showSelctTableView];
}


-(void)showSelctTableView {
    //1. 获取当前应用的主窗口
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    // 浮现动画
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.top = (kYQPScreenHeight - BGVIEW_H)-kHeight_Botton_SafeArea;
    }];
    
}

-(void)cancelSelctTableView {
    
    // 关闭动画
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.top = kYQPScreenHeight + BGVIEW_H;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma makr -- lazy
-(UIView *)bgView {
    if (!_bgView) {
        self.titleArr = NSMutableArray.new;
        if (!IsStrEmpty(self.model.refundExplain)&&self.model.refundExplain.intValue != 0) {
            [self.titleArr addObject:kYQLS(@"7天无理由退款")];
        }
        
        if (!IsStrEmpty(self.model.freightInsurance)&&self.model.freightInsurance.intValue != 0) {
            [self.titleArr addObject:kYQLS(@"运费险")];
        }
        
        if (!IsStrEmpty(self.model.quicknessRefund)&&self.model.quicknessRefund.intValue != 0) {
            [self.titleArr addObject:kYQLS(@"极速退款")];
        }
//        self.titleArr = @[kYQLS(@"7天无理由退款"),kYQLS(@"运费险"),kYQLS(@"极速退款")];
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kYQPScreenHeight+BGVIEW_H, kYQPScreenWidth, BGVIEW_H)];
        _bgView.backgroundColor = UIColor.whiteColor;
        [UIView roundedSetView:_bgView cornerRadius:12 Type:(kRoundedSetViewTypeTRL)];
        [self addSubview:_bgView];
        
        [_bgView addSubview:self.bottonView];
        [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(49);
        }];
        _titleLb = [UILabel new];
        _titleLb.textColor = kAPPMainTitleColor;
        _titleLb.font = kSystemMedium(17);
        _titleLb.text = kYQLS(@"商品说明");
        [_bgView addSubview:self.titleLb];
        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(24);
            make.height.mas_equalTo(24);
        }];
        [self.selectTableView registerNib:[UINib nibWithNibName:@"MYShopDetailsExplainCell" bundle:nil] forCellReuseIdentifier:@"MYShopDetailsExplainCell"];
        [_bgView addSubview:self.selectTableView];
        [self.selectTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLb.mas_bottom).mas_equalTo(18);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(self.bottonView.mas_top).mas_equalTo(0);
        }];
    }
    return _bgView;
}

-(MYShopDetailsSpecificationsSelectBottonBtn *)bottonView {
    if (!_bottonView) {
        _bottonView = [MYShopDetailsSpecificationsSelectBottonBtn viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        ws(self);
        [_bottonView.determineBtn addTapAction:^(id  _Nonnull sender) {
            [weakself cancelSelctTableView];
        }];
    }
    return _bottonView;
}
#pragma makr -- lazy end

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYShopDetailsExplainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopDetailsExplainCell"];
    cell.titleLb.text = self.titleArr[indexPath.section];
    cell.contentLb.text = kYQLS(@"满足7天无理由退换货申请的前提下,包邮商品需要卖家承担拖货邮费, 非包邮商品需要卖家承担发货和退货邮费");
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"MYShopDetailsExplainCell" configuration:^(MYShopDetailsExplainCell *cell) {
        cell.titleLb.text = self.titleArr[indexPath.section];
        cell.contentLb.text = kYQLS(@"满足7天无理由退换货申请的前提下,包邮商品需要卖家承担拖货邮费, 非包邮商品需要卖家承担发货和退货邮费");
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
@end
